import { makeUpZero } from ".."

/**
 * 计算两个日期之间的天数
 * @param {string|number,Date} fDate 开始日期
 * @param {string|number,Date} lDate 结束日期
 * @returns {number} 两个日期之间的天数
 */
export function calcDateDay(fDate = new Date(), lDate = new Date()) {
	var date1 = new Date(fDate)
	var date2 = new Date(lDate)
	var timeDiff = Math.abs(date2.getTime() - date1.getTime())
	var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24))
	return diffDays
}
/**
 * 判断是否是闰年
 * @param {string|number} year
 * @returns {boolean}
 */
export const isLeapYear = year => {
	return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0
}
/**
 * 每个月的天数
 * @param {Date|string|number} date 日期
 */
export function getMonthDay(date) {
	date = new Date(date)
	const monthDay = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
	// 判断date是否是一个有效的日期
	if (date === "Invalid Date") {
		console.error("getMonthDay: 日期格式错误")
		return null
	}
	if (month == 2) {
		if (isLeapYear(date.getFullYear())) {
			monthDay[1] = 29
		}
	}
	return monthDay[month - 1]
}

/**
 * 格式化时间戳
 * @param {number | string} timestamps 需要格式化时间戳
 * @returns {number}
 */
export function formattingTimestamps(timestamps = +new Date()) {
	timestamps = timestamps + ""
	if (timestamps.length < 13) {
		timestamps = makeUpZero(timestamps, 13)
	} else if (timestamps.length > 13) {
		timestamps = timestamps.slice(0, 13)
	}
	return +timestamps
}
